import { Service } from 'egg';

/**
 * Test Service
 */
export default class UserService extends Service {

  /**
   * sayHi to you
   * @param data - your name
   */
  // 增加
 public  async add(data) {
    const {ctx}=this;
       let res=await ctx.model.User.create({...data,create_time:new Date(),update_time:new Date()});
     console.log("返回", res);
    return `hi, ${res}`;
  }

  //按条件搜索
   public  async show() {
       const {ctx}=this;
      //  $or或者条件运算符   limit()查询数   sort()排序
       let res=await ctx.model.User.find({
        $or:[
          {title:/zdcy1122/},{create_time:{$gt:20055}}
        ]
      }).limit(5).sort({update_time:1});
     console.log("返回", res,'11',res.length);
    return res;
  }
  public async delete(id){
    const { ctx}= this;
    let res=await ctx.model.User.remove({_id:id})
    console.log("删除",res);
    return res
  }
  public async upload(updata){
    const { ctx}= this;
    const {id,data}= updata;
    let res=await ctx.model.User.update({title:id},{$set:data},{multi:true});
    return res
  } 
  async addUser(user){
    const { ctx } = this;
    let re=await ctx.model.User.find({username:user.username})||[]
    if (re.length<=0) {
      let res=await ctx.model.User.create(user)
    return res
    }
    return "用户已存在"
  
  }
}
